package common.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * @Author: gcr
 * @Date: 2023/9/12 16:37
 */
@Service
@Slf4j
public class DownloadUtil {

    @Resource
    HttpServletRequest request;

    @Resource
    HttpServletResponse response;


    public static void main(String[] args) {


    }

    /**
     * 下载文件
     *
     * @param filePath 文件路径(服务器上的) 真实全路径
     * @return
     */
    public void download(String filePath) throws UnsupportedEncodingException {
        // 文件名
        if (filePath != null) {
            log.info("下载路径+" + filePath);
            //从下载路径中获取文件名
            String[] strings = filePath.split("/");
            String fileName = strings[strings.length - 1];
            //设置文件路径
            File file = new File(filePath);
            //File file = new File(realPath , fileName);
            if (file.exists()) {
                // 设置强制下载不打开
                response.setContentType("application/force-download");
                //处理中问乱码
                fileName = URLEncoder.encode(fileName, "UTF-8");
                response.setCharacterEncoding("UTF-8");

                // 设置文件名
                response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    /**
     * 下载文件
     *
     * @param filePath 文件路径(服务器上的) 真实全路径
     * @return
     */
    public void downloadWithFileName(String filePath, String fileName) throws UnsupportedEncodingException {
        // 文件名
        if (filePath != null) {
            log.info("下载路径+" + filePath);
            //从下载路径中获取文件名
            //设置文件路径
            File file = new File(filePath);
            if (file.exists()) {
                // 设置强制下载不打开
                response.setContentType("application/force-download");
                //处理中问乱码
                fileName = URLEncoder.encode(fileName, "UTF-8");
                response.setCharacterEncoding("UTF-8");

                // 设置文件名
                response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
